// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy

using System;
using System.Globalization;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace EntLibContrib.Logging.Parser
{
    public class TimeStampFunctionParser
    {
        /// <summary>
        /// Constructor to initialize a new instance of the class.
        /// </summary>
        public TimeStampFunctionParser(string template, IFormatProvider formatProvider)
        {
            StringBuilder templateBuilder = new StringBuilder(template);
            string functionArg = GetArgAndUpdateTemplate(templateBuilder);
            parsingTemplate = templateBuilder.ToString();
            if (functionArg == null)
            {
                if (formatProvider == null)
                    theTimeStampParser = new TimeStampParser();
                else
                    theTimeStampParser = new TimeStampParser(formatProvider);
            }
            else
            {
                if (functionArg == TimeStampParserFraction.TimeStampFormat)
                {
                    if (formatProvider == null)
                        theTimeStampParser = new TimeStampParserFraction();
                    else
                        theTimeStampParser = new TimeStampParserFraction(formatProvider);
                }
                else if (functionArg == TimeStampParserLocal.TimeStampFormat)
                {
                    if (formatProvider == null)
                        theTimeStampParser = new TimeStampParserLocal();
                    else
                        theTimeStampParser = new TimeStampParserLocal(formatProvider);
                }
                else
                {
                    if (formatProvider == null)
                        theTimeStampParser = new TimeStampParserFormat(functionArg);
                    else
                        theTimeStampParser = new TimeStampParserFormat(functionArg, formatProvider);
                }
            }
        }
        private static string GetArgAndUpdateTemplate(StringBuilder messageBuilder)
        {
            const string startDelimiter = "{timestamp(";
            const string endDelimiter = ")}";
            int startPos = 0;
            while (startPos < messageBuilder.Length)
            {
                string messageString = messageBuilder.ToString();
                if (messageString.IndexOf(startDelimiter) == -1)
                {
                    break;
                }

                int tokenStartPos = messageString.IndexOf(startDelimiter, startPos) + startDelimiter.Length;
                int endPos = messageString.IndexOf(endDelimiter, tokenStartPos);
                string tokenTemplate = messageString.Substring(tokenStartPos, endPos - tokenStartPos);

                string tokenToReplace = startDelimiter + tokenTemplate + endDelimiter;
                startPos = messageBuilder.ToString().IndexOf(tokenToReplace);

                messageBuilder.Replace(tokenToReplace, "{timestamp}");

                return tokenTemplate;
            }
            return null;
        }


        private string parsingTemplate;
        public string ParsingTemplate
        {
            get { return parsingTemplate; }
        }

        private ITimeStampParser theTimeStampParser;
        public ITimeStampParser TimeStampParser
        {
            get { return theTimeStampParser; }
        }
    }
}
